package com.gifts.ui.page.login;

import java.io.InputStream;
import java.util.Scanner;


import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import com.gifts.service.FacebookService;
import com.gifts.ui.page.common.Page;
/**
 * FacebookLoginPage
 * @author dmaung
 *
 */
public class FacebookLoginPage extends Page {
	private Browser browser;
	FacebookService facebookService = FacebookService.getInstance();
	
	
	public FacebookLoginPage(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		
		createBrowser();
		
		this.setLayout(new FillLayout());
		setSize(new Point(800, 441));
	}

	/**
	 * This method initializes browser	
	 *
	 */
	private void createBrowser() {
			
		browser = new Browser(this, SWT.EMBEDDED);
		System.out.println("FacebookLoginURL:" + facebookService.getFacebookLoginURL());
		browser.setUrl(facebookService.getFacebookLoginURL());
	
		browser.addLocationListener(new LocationListener() {
		public void changed(LocationEvent event) {
			
			System.out.println("Event Location" + event.location);
			int index = event.location.indexOf("code=");
			if(index != -1) {
				System.out.println("Code=" + event.location);
				String code = event.location.substring(index+5);
				facebookService.authenticateAccessToken(code);
				
			
			
			
			}
			
			
			
			
		}
		public void changing(LocationEvent event) {
		}
	});
		
		
		
		
		
		
	}

}
